define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    var Controller = {
        index: function () {
            var supplier_id = GetQueryString('supplier_id');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/index?supplier_id='+supplier_id,
                    add_url: 'activity/add',
                    edit_url: '',
                    del_url: '',
                    multi_url: 'activity/multi',
                    table: 'activity',
                }
            });

            var sitetable = $("#sitetable");
            sitetable.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                       /* {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},*/
                        {field: 'supplier_name', title: __('Supplier_id')},
                        {field: 'poster', title: __('Poster'), operate:false,formatter: Table.api.formatter.image},
                        {field: 'name', title: '场地名称'},
                        {field: 'address', title: '场地地址'},
                        {field: 'max_num', title: '可承载人数'},
                        {field: 'fee', title: '场地费用'},
                        {field: 'is_house_text', title:'是否包含食宿', operate:false},
                        {field: 'square', title: '场地面积', operate:false},
                        {field: 'create_time', title: __('create_time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', options: {}},
                        {field: 'update_time', title: __('Update_time'), formatter: Table.api.formatter.datetime, operate:false},
                        {field: 'operate', title: __('Operate'), table: sitetable, 
                            buttons: [{
                                    name: 'detail',
                                    text: __('Detail'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'activity/detail'
                                }],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(sitetable);

            var activitytable = $("#activitytable");
            // 初始化表格
            activitytable.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                       /* {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},*/
                        {field: 'supplier_name', title: __('Supplier_id')},
                        {field: 'poster', title: __('Poster'), operate:false,formatter: Table.api.formatter.image},
                        {field: 'name', title: '活动名称'},
                        {field: 'category_text', title: '活动类别', operate:false},
                        {field: 'time', title: '活动时长'},
                        {field: 'fee', title: '活动价格'},
                        {field: 'max_num', title: '可承载人数'},
                        {field: 'include', title: '费用包含', operate:false},
                        {field: 'city_name', title: '城市'},
                        {field: 'site_name', title:'场地名称', operate:false},
                        {field: 'site_address', title:'场地地址', operate:false},
                       /* {field: 'course_plan', title: '活动安排', operate:false},*/
                        {field: 'create_time', title: __('create_time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', options: {}},
                        {field: 'update_time', title: __('Update_time'), formatter: Table.api.formatter.datetime, operate:false},
                        {field: 'operate', title: __('Operate'), table: activitytable, 
                            buttons: [{
                                    name: 'detail',
                                    text: __('Detail'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'activity/detail'
                                }],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(activitytable);

            var mastertable = $("#mastertable");
            mastertable.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                       /* {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},*/
                        {field: 'supplier_name', title: __('Supplier_id')},
                        {field: 'poster', title: __('Poster'), operate:false,formatter: Table.api.formatter.image},
                        {field: 'name', title: '课程名称'},
                        {field: 'category_text2', title: '课程类别', operate:false},
                        {field: 'time', title: '课程时长', formatter: Table.api.formatter.test},
                        {field: 'fee', title: '课程价格'},
                        /*{field: 'course_plan', title: '课程安排', operate:false},*/
                        {field: 'city_name', title: '城市'},
                        {field: 'notes', title: '课程须知', operate:false},
                        {field: 'create_time', title: __('create_time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', options: {}},
                        {field: 'update_time', title: __('Update_time'), formatter: Table.api.formatter.datetime, operate:false},
                        {field: 'operate', title: __('Operate'), table: mastertable, 
                            buttons: [{
                                    name: 'detail',
                                    text: __('Detail'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'activity/detail'
                                }],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(mastertable);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                test: function (value, row, index) {
               /*   alert("value = " + value);
                   alert("row = " + row);
                    alert("index = " + index);*/
                    return index;
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});